package com.edu.atams.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.atams.common.dto.SelectClassDto;
import com.edu.atams.common.lang.Result;
import com.edu.atams.entity.*;
import com.edu.atams.entity.Class;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/class")
public class ClassController extends BaseController {

    @GetMapping("/list")
    public Result list(Integer year, String gradeName, String name){
        Page<Class> page = classService.page(getPage(),new QueryWrapper<Class>()
                .like(StrUtil.isNotBlank(name),"name",name)
                .in(year != null,
                        "grade_id", gradeService.list(new QueryWrapper<Grade>()
                                .like("year",year)).stream().map(Grade::getId).collect(Collectors.toList()))
                .in(StrUtil.isNotBlank(gradeName),
                        "grade_id", gradeService.list(new QueryWrapper<Grade>()
                                .like("name",gradeName)).stream().map(Grade::getId).collect(Collectors.toList()))
        );
        page.getRecords().forEach(c -> c.setGrade(gradeService.getById(c.getGradeId())));
        page.getRecords().forEach(c -> c.setStudentNum((studentClassService.list(new QueryWrapper<StudentClass>().eq("class_id", c.getId()))).size()));


        return Result.succ(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Class cclass){
        List<Class> list = classService.list(new QueryWrapper<Class>().eq("name", cclass.getName()).eq("code", cclass.getCode()).eq("grade_id",cclass.getGradeId()));
        if (list.size() > 0){
            return  Result.fail("班级已存在");
        }else {
            classService.save(cclass);
            return Result.succ("");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Class cclass){
        classService.updateById(cclass);
        return Result.succ("");
    }

    @PostMapping("/remove")
    @Transactional
    public Result remove(@RequestBody Integer[] ids){
        classService.removeByIds(Arrays.asList(ids));
        return Result.succ("");

    }

    //用于下拉框回显
    @GetMapping("/listAll")
    public Result listAll(){
        List<Class> classList = classService.list();
        List<Grade> grades = new ArrayList<>();
//        for (Grade grade : list) {
//            Grade grade1 = new Grade();
//            grade1.setId(grade.getId());
//            grade1.setYear(grade.getYear());
//            grade1.setName(grade.getYear()+" "+grade.getName());
//            grades.add(grade1);
//        }
        for (Class aClass : classList) {
            Grade grade = gradeService.getOne(new QueryWrapper<Grade>().eq("id", aClass.getGradeId()));
            aClass.setGrade(grade);
            List<StudentClass> studentClasses = studentClassService.list(new QueryWrapper<StudentClass>().eq("class_id", aClass.getId()));
            aClass.setStudentNum(studentClasses.size());
        }
        return Result.succ(classList);
    }

    //根据班级表ID获取所有当前班级学生的信息
    @GetMapping("/getStudents/{id}")
    public Result getStudents(@PathVariable("id") Integer classId){
        List<StudentClass> studentClasses = studentClassService.list(new QueryWrapper<StudentClass>().eq("class_id", classId));
        if(studentClasses.size() > 0){
            List<Integer> studentId = studentClasses.stream().map(studentClass -> studentClass.getStudentId()).collect(Collectors.toList());
            List<Student> students = studentService.listByIds(studentId);
            return Result.succ(students);
        }else {
            return Result.fail("该班级没有学生");
        }

    }








}
